<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 8px;
            border: 1px solid #ddd;
        }

        th {
            background-color: #f2f2f2;
        }

        .pagination {
            margin-top: 16px;
            text-align: center;
        }

        .pagination a {
            display: inline-block;
            padding: 4px 8px;
            margin-right: 4px;
            border: 1px solid #ddd;
            text-decoration: none;
            color: #333;
        }

        .pagination a.active {
            background-color: #f2f2f2;
        }
    </style>
</head>

<body>
    <h1>留言板</h1>
    <?php
    include 'conn.php';

    // 分页设置
    $perPage = 5; // 每页显示的留言数量
    $currentPage = isset($_GET['page']) ? $_GET['page'] : 1; // 当前页码

    // 查询留言总数
    $totalMessages = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM messages"));

    // 计算总页数
    $totalPages = ceil($totalMessages / $perPage);

    // 防止页码超出范围
    if ($currentPage < 1) {
        $currentPage = 1;
    } elseif ($currentPage > $totalPages) {
        $currentPage = $totalPages;
    }

    // 计算偏移量
    $offset = ($currentPage - 1) * $perPage;

    // 查询当前页的留言列表
    $sql = "SELECT * FROM messages ORDER BY created_at DESC LIMIT $offset, $perPage";
    $result = mysqli_query($conn, $sql);

    // 遍历查询结果并输出留言
    if (mysqli_num_rows($result) > 0) {
        echo '<table>';
        echo '<tr>
                <th>序号</th>
                <th>发件人</th>
                <th>时间</th>
                <th>留言内容</th>
                <th>操作</th>
              </tr>';

        $startNumber = ($currentPage - 1) * $perPage + 1;
        while ($row = mysqli_fetch_assoc($result)) {
            echo '<tr>';
            echo '<td>' . $startNumber . '</td>';
            echo '<td>' . $row['sendname'] . '</td>';
            echo '<td>' . date("Y-m-d H:i:s", $row['created_at']) . '</td>';
            echo '<td>' . $row['content'] . '</td>';
            echo '<td><a href="deleteMessage.php?id=' . $row['id'] . '">删除</a></td>';
            echo '</tr>';
            $startNumber++;
        }

        echo '</table>';

        // 显示分页链接
        echo '<div class="pagination">';
        for ($i = 1; $i <= $totalPages; $i++) {
            $isActive = $i == $currentPage ? 'active' : '';
            echo '<a class="' . $isActive . '" href="?page=' . $i . '">' . $i . '</a>';
        }
        echo '</div>';
    } else {
        echo '暂无留言';
    }

    // 释放查询结果内存
    mysqli_free_result($result);

    // 关闭数据库连接
    mysqli_close($conn);
    ?>

    <!-- 显示留言表单 -->
    <h2>添加留言</h2>
    <form action="addMessage.php" method="POST">
        <input type="text" placeholder="发件人姓名" name="sendname" required value="张娜">
        <br><br>
        <textarea name="content" rows="4" cols="50" placeholder="请输入留言内容" required></textarea>
        <br>
        <input type="submit" value="提交留言">
    </form>
</body>

</html>
